// pages/signinList/signinList.js
const app = getApp()
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    actSigns: [],
    id: '',
    item: '',
    user: '',
    number: '',
    show: false,
    mode: false,
    islocation:true,
    dKm:0,
    issignbtn:true,
    issignbtn1:true,
  },
  getlist() {
    var that = this
    app.req({
      url: "wechat/activity/activityDetail",
      method: "post",
      data: {
        id: that.data.id
      },
      success: function(res) {
        if (200 == res.code) {
          that.setData({
            actSigns: res.data.actSigns,
            item: res.data
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    });
  },
  signbtn(e) {
    var that = this
    if (that.data.issignbtn){
      that.setData({
        issignbtn:false,
      })
    var time = util.getTime(new Date());
    var index = e.currentTarget.dataset.index
    var lat = null
    var log = null
    var lat1 = that.data.actSigns[index].latitude
    var log1 = that.data.actSigns[index].longitude
    console.log('签到1', e)
    if (that.data.actSigns[index].startTime <= time && time <= that.data.actSigns[index].endTime) {
      wx.getLocation({
        type: 'gcj02',
        success: function(res) {
          console.log('当前坐标', res, lat1, log1)
          that.setData({
            islocation: true,
          })
          var lat = res.latitude
          var log = res.longitude
          var km = that.distance(lat, log, lat1, log1)
          console.log('距离', km)
          if (km <= that.data.actSigns[index].signRange) {
            app.req({
              url: "wechat/sign",
              method: "post",
              data: {
                signId: that.data.actSigns[index].id,
                latitude: lat,
                longitude: log,
                address: '',
              },
              success: function(res) {
                if (200 == res.code) {
                  that.setData({
                    number: res.data.num,
                    mode: true,
                    // issignbtn: true
                  })
                  that.getlist()
                } else {
                  that.setData({
                    issignbtn: true
                  })
                  wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 2000
                  })
                }
              }
            })

          } else {
            wx.showModal({
              title: '提示',
              content: '请您到达指定的签到地点后，再点击签到按钮',
              cancelText: '返回',
              cancelColor: '#999999',
              confirmText: '再试一次',
              confirmColor: '#2168EB',
              success(res) {
                if (res.confirm) {
                  that.setData({
                    issignbtn: true
                  })
                  that.signbtn1(index)
                } else if (res.cancel) {
                  that.setData({
                    issignbtn: true
                  })
                }
              }
            })
          }
        },
        fail() {
          that.setData({
            islocation: false,
            issignbtn: true
          })

        }
      })    
    } else {
      wx.showModal({
        title: '提示',
        content: '请您在指定时间段内签到',
        cancelText: '返回',
        cancelColor: '#999999',
        confirmText: '再试一次',
        confirmColor: '#2168EB',
        success(res) {
          if (res.confirm) {
            that.setData({
              issignbtn: true
            })
            that.signbtn1(index)
          } else if (res.cancel) {
            that.setData({
              issignbtn: true
            })
          }
        }
      })
    }
  }
  },
  signbtn1(index) {
    var that = this
    if(that.data.issignbtn1){
      that.setData({
        issignbtn1:false
      })
    var time = util.getTime(new Date());
    var lat = null
    var log = null
    var lat1 = that.data.actSigns[index].latitude
    var log1 = that.data.actSigns[index].longitude
    console.log('签到3')
    if (that.data.actSigns[index].startTime <= time && time <= that.data.actSigns[index].endTime) {
      wx.getLocation({
        type: 'gcj02',
        success: function(res) {
          console.log('当前坐标', res, lat1, log1)
          var lat = res.latitude
          var log = res.longitude
          var km = that.distance(lat, log, lat1, log1)
          console.log('距离', km)
          if (km < that.data.actSigns[index].signRange) {
            app.req({
              url: "wechat/sign",
              method: "post",
              data: {
                signId: that.data.actSigns[index].id,
                latitude: lat,
                longitude: log,
                address: '',
              },
              success: function(res) {
                if (200 == res.code) {
                  that.setData({
                    number: res.data.num,
                    mode: true,
                    // issignbtn1:true,
                  })
                  that.getlist()
                } else {
                  that.setData({
                    issignbtn1: true,
                  })
                  wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 2000
                  })
                }
              }
            });
          } else {
            wx.showModal({
              title: '提示',
              content: '请您到达指定的签到地点后，再点击签到按钮',
              cancelText: '返回',
              cancelColor: '#999999',
              confirmText: '再试一次',
              confirmColor: '#2168EB',
              success(res) {
                if (res.confirm) {
                  that.setData({
                    issignbtn1: true,
                  })
                  that.signbtn1(index)
                } else if (res.cancel) {
                  that.setData({
                    issignbtn1: true,
                  })
                }
              }
            })
          }
        }
      })

    } else {
      wx.showModal({
        title: '提示',
        content: '请您在指定时间段内签到',
        cancelText: '返回',
        cancelColor: '#999999',
        confirmText: '再试一次',
        confirmColor: '#2168EB',
        success(res) {
          if (res.confirm) {
            that.setData({
              issignbtn1: true,
            })
            that.signbtn1(index)
          } else if (res.cancel) {
            that.setData({
              issignbtn1: true,
            })
          }
        }
      })
    }
  }
  },
  signbtn2(e) {
    var that = this
    if(that.data.issignbtn){
      that.setData({
        issignbtn: false
      })
    var time = util.getTime(new Date());
    var index = e.currentTarget.dataset.index
    var lat = null
    var log = null
    var lat1 = that.data.actSigns[index].latitude
    var log1 = that.data.actSigns[index].longitude
    console.log('签到2', e)
    if (that.data.actSigns[index].startTime <= time && time <= that.data.actSigns[index].endTime) {
      if (e.detail.authSetting['scope.userLocation']) {
      wx.getLocation({
        type: 'gcj02',
        success: function (res) {
          console.log('当前坐标', res, lat1, log1)
          that.setData({
            islocation: true,
          })
          var lat = res.latitude
          var log = res.longitude
          var km = that.distance(lat, log, lat1, log1)
          console.log('距离', km)
          if (km < that.data.actSigns[index].signRange) {
            app.req({
              url: "wechat/sign",
              method: "post",
              data: {
                signId: that.data.actSigns[index].id,
                latitude: lat,
                longitude: log,
                address: '',
              },
              success: function (res) {
                if (200 == res.code) {
                  that.setData({
                    number: res.data.num,
                    mode: true,
                    // issignbtn:true,
                  })
                  that.getlist()
                } else {
                  that.setData({
                    issignbtn: true
                  })
                  wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 2000
                  })
                }
              }
            })

          } else {
            wx.showModal({
              title: '提示',
              content: '请您到达指定的签到地点后，再点击签到按钮',
              cancelText: '返回',
              cancelColor: '#999999',
              confirmText: '再试一次',
              confirmColor: '#2168EB',
              success(res) {
                if (res.confirm) {
                  that.setData({
                    issignbtn: true
                  })
                  that.signbtn1(index)
                } else if (res.cancel) {
                  that.setData({
                    issignbtn: true
                  })
                 }
              }
            })
          }
        },
        fail() {
          that.setData({
            islocation: false,
            issignbtn:true
          })

        }
      })


    } else {
      wx.showModal({
        title: '提示',
        content: '请您在指定时间段内签到',
        cancelText: '返回',
        cancelColor: '#999999',
        confirmText: '再试一次',
        confirmColor: '#2168EB',
        success(res) {
          if (res.confirm) {
            that.setData({
              issignbtn: true
            })
            that.signbtn1(index)
          } else if (res.cancel) { 
            that.setData({
              issignbtn: true
            })
          }
        }
      })
    }
    }
  }
  },
  cancle() {
    this.setData({
      mode: false,
      issignbtn:true,
      issignbtn1:true,
    })
    this.getlist()
  },
  distance: function(la1, lo1, la2, lo2) {
    var La1 = la1 * Math.PI / 180.0;
    var La2 = la2 * Math.PI / 180.0;
    var La3 = La1 - La2;
    var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
    s = s * 6378137;
    // s = Math.round(s * 10000) / 10000;
    s = s.toFixed(2);
    return s;
  },
  getnum(e) {
    var that = this
    var signId = e.currentTarget.dataset.id
    app.req({
      url: "wechat/sign",
      method: "post",
      data: {
        signId: signId,
        activityId: that.data.id
      },
      success: function(res) {
        if (res.code == 200) {
          that.setData({
            number: res.data.num
          })
        }
      }
    })
  },
  // getkm(){
  //   var that = this
  //   app.req({
  //     url: "wechat/dictionary/listBydictType",
  //     method: "post",
  //     data: {
  //       dictType:'SIGN_SCOPE'
  //     },
  //     success: function (res) {
  //       if (res.code == 200) {
  //         that.setData({
  //           dKm: res.data[0].dictValue
  //         })
  //         console.log('字典项',res)
  //       }
  //     }
  //   })
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    that.setData({
      id: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    app.getUserInfo(function(userInfo) {
      that.setData({
        user: userInfo
      })
    })
    wx.getSetting({
      success: function (res) {
        console.log(res)
        if (res.authSetting['scope.userLocation'] == false) {
          that.setData({
            islocation: false
          })
        }else{
          that.setData({
            islocation: true
          })
        }
      }
    })
    that.setData({
      show: false,
      issignbtn:true,
      issignbtn1:true,
    })
    // that.getkm()
    that.getlist()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  preventTouchMove: function(e) {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(e) {
    var that = this
    console.log(e)
    if (e.target.dataset.type == 1) {
      var signId = e.target.dataset.id
      app.req({
        url: "wechat/activity/rank",
        method: "post",
        data: {
          signId: signId,
          activityId: that.data.id
        },
        success: function(res) {
          if (res.code == 200) {
            that.setData({
              number: res.data.num,
              callback: function() {}
            })
          }
        }
      })
    }
    that.setData({
      mode: false,
      issignbtn:true,
      issignbtn1:true,
      show: true,
      callback: function() {}
    })
    console.log('分享分享')
    return {
      title: '活动：' + that.data.item.topic,
      path: '/pages/register/register?&id=' + that.data.id + '&isShare=1',
    }
  }
})